package com.pro.h5case.front.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.h5case.common.controller.BaseController;
import com.pro.h5case.common.model.BOrder;
import com.pro.h5case.common.service.BOrderService;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController{
	@Autowired
	BOrderService bOrderService;
	@Autowired
	private JavaMailSenderImpl mailSender;
	
	@RequestMapping(value="/addOrder.html",method=RequestMethod.POST)
	public @ResponseBody void insertOrder(HttpServletRequest request,BOrder bOrder){
		if (bOrder.getOrderDescription().getBytes().length>200) {
			logger.info("there too long description");
		}else if(bOrder.getOrderName().getBytes().length>20){
			logger.info("there too long orderName");
		}else if(bOrder.getOrderPhone().getBytes().length>60){
			logger.info("there too long orderPhone");
		}else{
			bOrder.setOrderCreateTime(new Date());
			bOrder.setOrderDeleteState((byte) 1);
			System.out.println(bOrder.getOrderPlanId()+bOrder.getOrderPlanTitle());
			int ret=bOrderService.insertBOrder(bOrder);
			if(ret>0){
				SimpleMailMessage message = new SimpleMailMessage();
				message.setFrom("h5case@xuanzhen-tech.com");
//				message.setTo("mai.yu@xuanzhen-tech.com");
				message.setTo("fuzhi.dai@xuanzhen-tech.com");
				String[] cc=new String[]{"eric.shen@xuanzhen-tech.com","mai.yu@xuanzhen-tech.com"};
				message.setCc(cc);
				message.setSubject("H5case客户定制需求");
				String text="客户姓名："+bOrder.getOrderName()+"\n客户联系方式："+bOrder.getOrderPhone()+"\n客户需求描述："+bOrder.getOrderDescription();
				if(bOrder.getOrderPlanId()!=null){
					text+="\n关联案例："+bOrder.getOrderPlanTitle()+"\n关联案例id："+bOrder.getOrderPlanId();
				}
				message.setText(text);
				mailSender.send(message);
			}
		}
	}
}
